<?php

class Invoice extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }
    /*
     * 开具类型 1个人 2企业
     */
    const RECEIPT_VAL_ONE = 1; //个人
    const RECEIPT_VAL_TWO = 2; //企业
    /*
     * 发票类型
     * 1增值税普通发票
     * 2增值税专用发票
     */
    const INVOICE_VAL_ONE = 1; //
    const INVOICE_VAL_TWO = 2; //

    /*
     *
     * 发票更新状态
     *
     */
    const STATUS = 3; //已完成
    public function index()
    {
        $act = $this->input->get_post('ajaxdata');
        $result = array('code' => 201, 'message' => '', 'result' => '');
        switch ($act) {
            case 'add': //添加发票
                $this->load->library('C_add_invoice');
                $sendData = array();
                $sendData['password'] = $this->lg_user['password'];
                $sendData['uid'] = $this->lg_user['id'];
                $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
                $sendData['channelId'] = $this->lg_user['channelId'];
                $sendData['userType'] = $this->lg_user['type'];

                $sendData['invoice_price'] = $this->input->get_post('invoice_price');
                $sendData['receipt_val'] = $this->input->get_post('receipt_val');
                $sendData['invoice_val'] = $this->input->get_post('invoice_val');
                if ($sendData['receipt_val'] == self::RECEIPT_VAL_ONE){  //开具类型为个人
                    $res = $this ->insetInvoice($sendData);
                    $result['msg'] = $res;
                    break;
                }else if ($sendData['receipt_val'] == self::RECEIPT_VAL_TWO && $sendData['invoice_val'] == self::INVOICE_VAL_ONE){ //开具类型为企业发票类型为增值税普通发票
                    $sendData['invoice_rise'] = $this->input->get_post('invoice_rise');
                    $res = $this ->insetInvoice($sendData);
                    $result['msg'] = $res;
                    break;
                }else if ($sendData['receipt_val'] == self::RECEIPT_VAL_TWO && $sendData['invoice_val'] == self::INVOICE_VAL_TWO){//开具类型为企业发票类型为增值税专用发票
                    $sendData['invoice_rise'] = $this->input->get_post('invoice_rise');
                    $sendData['credit_code'] = $this->input->get_post('credit_code');
                    $sendData['account_name'] = $this->input->get_post('account_name');
                    $sendData['account_bank'] = $this->input->get_post('account_bank');
                    $sendData['bank_account'] = $this->input->get_post('bank_account');
                    $sendData['address'] = $this->input->get_post('address');
                    $sendData['telephone'] = $this->input->get_post('telephone');
                    $res = $this ->insetInvoice($sendData);
                    $result['msg'] = $res;
                    break;
                }
                break;
            case 'update': //更新发票状态
                $this->load->library('C_invoice_update');
                $sendData = array();
                $sendData['password'] = $this->lg_user['password'];
                $sendData['uid'] = $this->lg_user['id'];
                $sendData['enterpriseId'] = $this->lg_user['enterpriseId'];
                $sendData['channelId'] = $this->lg_user['channelId'];
                $sendData['userType'] = $this->lg_user['type'];
                $sendData['invoice_id'] = $this->input->get_post('invoice_id');
                $sendData['status'] = self::STATUS;
                //制造接口请求
                $url = $this->c_invoice_update->set_call_items($sendData);
                $param = $this->c_invoice_update->get_call_items();
                $rs = $this->restcurl->put($url, $param);
                if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1 && $rs['ErrorMsg'] == '') {
                    $result['msg'] = '已完成';
                }
                break;
            default:
                break;
        }
        echo json_encode($result);
        die;
    }
    public function insetInvoice($sendData){
        //制造接口请求
        $url = $this->c_add_invoice->set_call_items($sendData);
        $param = $this->c_add_invoice->get_call_items();
        $rs = $this->restcurl->post($url, $param);
        if ($rs['ErrorCode'] == 0 && $rs['Success'] == 1 && $rs['ErrorMsg'] == '') {
            return '取消订单成功!';
        }
    }

}
